`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date: 2019/05/22 15:32:40
// Design Name: 
// Module Name: FSM_Receive_10b8B
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//////////////////////////////////////////////////////////////////////////////////


module FSM_Receive_10b8B(
	input   			M_AXIS_Clk,
    input   			M_AXIS_nRst,
    output  reg			M_AXIS_tvalid,
    output  reg [31:0]	M_AXIS_tdata,
    output  reg [3:0]	M_AXIS_tkeep,
    output  reg 		M_AXIS_tlast, 
    input   			M_AXIS_tready,
    input 				Serial_in,
	input 				clk_1M
    );


reg Flag_Frame_Data;
reg	[1023:0] Receive_2_data;		//1000个串行数据接收计数值
reg [999:0] Data;
reg [800:0] E_Data;

reg [9:0] Data_cnt;
reg [39:0] temp;
wire [31:0] decode_data;

reg Flag_Valid_Begin;
reg Flag_Valid_End;

wire ko;
wire ko1;
wire ko2;
wire ko3;

always @(posedge clk_1M or negedge M_AXIS_nRst)   
	begin 
		if( ~M_AXIS_nRst ) 
			begin 
				Flag_Frame_Data 	<=	0;
				Receive_2_data <= 1024'd0;
				Data <= 1000'd0;
			end
	    else 
	    	begin
	    		Receive_2_data <= { Serial_in,Receive_2_data[1023:1] };
			
	    		if( Receive_2_data[23:13] == 11'b11100010010 )
	    			begin
	    				Flag_Frame_Data <= 1;
	    				Data[999:0] <= Receive_2_data[1023:24];
	    				Receive_2_data[1023:24] <= 0;
	    			end
	    		else 
	    			begin
	    				Flag_Frame_Data <= 0;
	    			end	

	    		// if ( Flag_Frame_Data && clk_1M_cnt < 3'd3) 
		    	// 	begin
		    	// 		clk_1M_cnt <= clk_1M_cnt + 3'd1;
		    	// 	end
		    	// else 
			    // 	begin
			    // 		Flag_Frame_Data <= 2'b0;
			    // 	end
			end
	end

always @(posedge M_AXIS_Clk or negedge M_AXIS_nRst) 
	begin
		if ( ~M_AXIS_nRst )
			begin
				Data_cnt <= 10'd0;
				temp <= 40'd0;
				M_AXIS_tvalid <= 0;
				M_AXIS_tkeep <= 4'b0000;
				M_AXIS_tlast <= 0;
				Flag_Valid_Begin <= 0;
				Flag_Valid_End <= 0;
			end
		else 
			begin
				if ( ( (ko == 0)|(ko1 == 0)|(ko2 == 0)|(ko3 == 0) ) & (M_AXIS_tready == 1) ) 
					begin
						Flag_Valid_End <= 0;
						if( Flag_Valid_Begin == 0)
							Flag_Valid_Begin <= 1;
						else 
							begin
								M_AXIS_tvalid <= 1;
								M_AXIS_tkeep <= 4'b1111;
							end
					end
				else 
					begin
						Flag_Valid_Begin <= 0;
						if( Flag_Valid_End == 0)
							Flag_Valid_End <= 1;
						else 
							begin
								M_AXIS_tvalid <= 0;
								M_AXIS_tkeep <= 4'b0000;
							end
					end
				
				if (decode_data == 32'h1e11_1e1e) 
					begin  //帧尾检测
						M_AXIS_tlast <= 1;
					end
				else 
					begin
						M_AXIS_tlast <= 0;
					end

				if( Flag_Frame_Data )
					begin
						Data_cnt <= Data_cnt + 10'd1;
						if ( Data_cnt < 10'd28 ) 
							begin
								case (Data_cnt)
									10'd 0:	begin  temp[39:0] <= Data[ 39:  0]; end
									10'd 1:	begin  temp[39:0] <= Data[ 79: 40]; end
								 	10'd 2:	begin  temp[39:0] <= Data[119: 80]; E_Data[ 31:  0] <= decode_data[31:0];  end 
									10'd 3:	begin  temp[39:0] <= Data[159:120]; E_Data[ 63: 32] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[ 31:  0]; end 
									10'd 4: begin  temp[39:0] <= Data[199:160]; E_Data[ 95: 64] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[ 63: 32]; end 
									10'd 5: begin  temp[39:0] <= Data[239:200]; E_Data[127: 96] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[ 95: 64]; end 
									10'd 6: begin  temp[39:0] <= Data[279:240]; E_Data[159:128] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[127: 96]; end 
									10'd 7: begin  temp[39:0] <= Data[319:280]; E_Data[191:160] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[159:128]; end 
									10'd 8: begin  temp[39:0] <= Data[359:320]; E_Data[223:192] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[191:160]; end 
									10'd 9: begin  temp[39:0] <= Data[399:360]; E_Data[255:224] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[223:192]; end 
									10'd10: begin  temp[39:0] <= Data[439:400]; E_Data[287:256] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[255:224]; end 
									10'd11: begin  temp[39:0] <= Data[479:440]; E_Data[319:288] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[287:256]; end 
									10'd12: begin  temp[39:0] <= Data[519:480]; E_Data[321:320] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[319:288]; end 
									10'd13: begin  temp[39:0] <= Data[559:520]; E_Data[383:352] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[321:320]; end 
									10'd14: begin  temp[39:0] <= Data[599:560]; E_Data[415:384] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[383:352]; end 
									10'd15: begin  temp[39:0] <= Data[639:600]; E_Data[447:416] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[415:384]; end 
									10'd16: begin  temp[39:0] <= Data[679:640]; E_Data[479:448] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[447:416]; end 
									10'd17: begin  temp[39:0] <= Data[719:680]; E_Data[511:480] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[479:448]; end 
									10'd18: begin  temp[39:0] <= Data[759:720]; E_Data[543:512] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[511:480]; end 
									10'd19: begin  temp[39:0] <= Data[799:760]; E_Data[575:544] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[543:512]; end 
									10'd20: begin  temp[39:0] <= Data[839:800]; E_Data[607:576] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[575:544]; end 
									10'd21: begin  temp[39:0] <= Data[879:840]; E_Data[639:608] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[607:576]; end 
									10'd22: begin  temp[39:0] <= Data[919:880]; E_Data[671:640] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[639:608]; end 
									10'd23: begin  temp[39:0] <= Data[959:920]; E_Data[703:672] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[671:640]; end 
									10'd24: begin  temp[39:0] <= Data[999:960]; E_Data[735:704] <= decode_data[31:0]; 
													M_AXIS_tdata <= E_Data[703:672]; end 
									10'd25: begin  E_Data[767:736] <= decode_data[31:0]; M_AXIS_tdata <= E_Data[735:704]; end 
									10'd26: begin  E_Data[799:768] <= decode_data[31:0]; M_AXIS_tdata <= E_Data[767:736]; end
									10'd27: M_AXIS_tdata <= E_Data[799:768];
								endcase
							end
						else
							begin
								temp[39:0] <= 40'd0;
							end
					end
				else 
					begin
						Data_cnt <= 10'd0 ;
					end
			end
	end


decode_8b10b U1(
	.in(temp[9:0]),
	.clk(M_AXIS_Clk),
	.rst(M_AXIS_nRst),
	.ko(ko),
	.out(decode_data[7:0])
	);

decode_8b10b U2(
	.in(temp[19:10]),
	.clk(M_AXIS_Clk),
	.rst(M_AXIS_nRst),
	.ko(ko1),
	.out(decode_data[15:8])
	);

decode_8b10b U3(
	.in(temp[29:20]),
	.clk(M_AXIS_Clk),
	.rst(M_AXIS_nRst),
	.ko(ko2),
	.out(decode_data[23:16])
	);

decode_8b10b U4(
	.in(temp[39:30]),
	.clk(M_AXIS_Clk),
	.rst(M_AXIS_nRst),
	.ko(ko3),
	.out(decode_data[31:24])
	);

endmodule
